/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.shinyray.org.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Logger;
import javax.faces.model.SelectItem;

/**
 *
 * @author alain
 */
public class AutoCompleteOrgDictionary {
    // list of orgnizations.
    private static List dictionary;
    //log
    private Logger logger=Logger.getLogger(AutoCompleteOrgDictionary.class.getName());
    public AutoCompleteOrgDictionary() {
        this.init();
    }

    /**
     * Comparator utility for sorting orginzation names.
     */
    public static final Comparator LABEL_COMPARATOR = new Comparator() {
        String s1;
        String s2;

        // compare method for city entries.
        public int compare(Object o1, Object o2) {

            if (o1 instanceof SelectItem) {
                s1 = ((SelectItem) o1).getLabel();
            } else {
                s1 = o1.toString();
            }

            if (o2 instanceof SelectItem) {
                s2 = ((SelectItem) o2).getLabel();
            } else {
                s2 = o2.toString();
            }
            // compare ingnoring case, give the user a more automated feel when typing
            return s1.compareToIgnoreCase(s2);
        }
    };

    public List getDictionary() {
        return dictionary;
    }



    
    /**
     * 初始化组织机构
     */
    private void init()
    {
        // Raw list of orgnizations,
        List orgList = null;
        orgList=new ArrayList();
        int i;
        OrgnizationVO org;
        for(i=0;i<5;i++)
        {
            org=new OrgnizationVO();
            org.setOrgCode("org_001_"+i);
            org.setOrgName("org_abe_"+i);
            orgList.add(org);
        }
        for(i=5;i<10;i++)
        {
            org=new OrgnizationVO();
            org.setOrgCode("org_"+i);
            org.setOrgName("org_adc_"+i);
            orgList.add(org);
        }
        //设置dictionary,元素为SelectItem
        if(orgList!=null)
        {
            dictionary=new ArrayList(orgList.size());
            for(i=0;i<orgList.size();i++)
            {
                org=(OrgnizationVO) orgList.get(i);
                if(org!=null && org.getOrgName()!=null)
                {
                    dictionary.add(new SelectItem(org, org.getOrgName()));
                }
            }//end for
        }//end if
        orgList.clear();
        // finally sort the list
        Collections.sort(dictionary, LABEL_COMPARATOR);

    }

}
